package com.mirzabros.chexmo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.mirzabros.chexmo.contant.Chexmo_Urls;
import com.mirzabros.chexmo.service.Chexmo_CheckPin_Service;
import com.mirzabros.chexmo.service.Chexmo_MobileNo_Check_Service;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Chexmo_Chk_Pin extends Activity{
	
	private ImageView logoimg;
	
	private boolean hasKeypad = false;
	PopupWindow popupWindow;
	
	EditText pinno;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.chexmo_enterpin);
		
		pinno = (EditText)findViewById(R.id.txt_pin);
		
		pinno.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if(!hasKeypad){
					hasKeypad = true;
					createMovableMenu();
				}

			}

		});
		
		logoimg = (ImageView)findViewById(R.id.img_logo);
		TextView phoneno = (TextView)findViewById(R.id.txt_phoneno_data);
		final Button btnchkin  = (Button)findViewById(R.id.btn_checkin);
		
		/*pinno.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					btnchkin.performClick();
				}
				return false;
			}
		});*/
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			final String user = extras.getString("user");
			final String pass = extras.getString("pass");
			final String code = extras.getString("code");
			final String PhoNumber = extras.getString("number");
			final String Logo = extras.getString("logo");
			
			phoneno.setText(PhoNumber);
			
			btnchkin.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Chexmo_CheckPin_Service mps = new Chexmo_CheckPin_Service(
							Chexmo_Chk_Pin.this,user,pass,code,PhoNumber,pinno.getText().toString(),Logo);
					mps.execute(new String[] { Chexmo_Urls.PinCheck });
				}
			});
			
			new URLCache().execute(Logo);
		}
	}
	
	//PSJ - Image Caching using browser cache
	
	class URLCache extends AsyncTask<String, Void, Bitmap> {
	    // Decode image in background.
	    @Override
	    protected Bitmap doInBackground(String... params) {
	    	URL url;
	    	Bitmap imgurl=null;
			try {
				Log.e("params[0]", params[0]);
				url = new URL(params[0]);
				URLConnection connection = null;
				connection = url.openConnection();
				connection.setUseCaches(true);
				
				connection.connect();
				
				imgurl = BitmapFactory.decodeStream( connection.getInputStream() );
				
				
				
			} catch (MalformedURLException e) {
				return null;
			} catch (IOException e) {
				return null;
			}
	        return imgurl;
	    }
	    
	    @Override
	    protected void onPostExecute(Bitmap result) {
	    	if (result != null){
	    		logoimg.setImageBitmap(result);
	    	}
	    }
	}
	//PSJ - Image Caching using browser cache
	
	private void createMovableMenu() {

		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View popupView = layoutInflater.inflate(R.layout.chexmo_fancy_keypad,
				null);
		popupWindow = new PopupWindow(popupView,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		Button btnDismiss = (Button) popupView.findViewById(R.id.btn_close);

		Button btnMove = (Button) popupView.findViewById(R.id.btn_Move);

		btnDismiss.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				hasKeypad = false;
				popupWindow.dismiss();
			}
		});

		//popupWindow.showAsDropDown(phonenumber, 50, -30);
		popupWindow.showAsDropDown(pinno, 250, 100);

		btnMove.setOnTouchListener(new OnTouchListener() {
			int orgX, orgY;
			int offsetX, offsetY;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					orgX = (int) event.getX();
					orgY = (int) event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					offsetX = (int) event.getRawX() - orgX;
					offsetY = (int) event.getRawY() - orgY;
					popupWindow.update(offsetX, offsetY, -1, -1, true);
					break;
				}
				return true;
			}
		});

		popupView.setFocusable(true);

		Button btn1 = (Button) popupView.findViewById(R.id.Button01);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "1");
			}
		});
		Button btn2 = (Button) popupView.findViewById(R.id.Button02);
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "2");
			}
		});
		Button btn3 = (Button) popupView.findViewById(R.id.Button03);
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "3");
			}
		});
		Button btn4 = (Button) popupView.findViewById(R.id.Button04);
		btn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "4");
			}
		});
		Button btn5 = (Button) popupView.findViewById(R.id.Button05);
		btn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "5");
			}
		});
		Button btn6 = (Button) popupView.findViewById(R.id.Button06);
		btn6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "6");
			}
		});
		Button btn7 = (Button) popupView.findViewById(R.id.Button07);
		btn7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "7");
			}
		});
		Button btn8 = (Button) popupView.findViewById(R.id.Button08);
		btn8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "8");
			}
		});
		Button btn9 = (Button) popupView.findViewById(R.id.Button09);
		btn9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "9");
			}
		});
		Button btn0 = (Button) popupView.findViewById(R.id.Button00);
		btn0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "0");
			}
		});
		Button btnStar = (Button) popupView.findViewById(R.id.ButtonStar);
		btnStar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "*");
			}
		});
		Button btnPound = (Button) popupView.findViewById(R.id.ButtonPound);
		btnPound.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText(pinno.getText().toString() + "#");
			}
		});
		Button btnBk = (Button) popupView.findViewById(R.id.ButtonBk);
		btnBk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText((!pinno.getText().toString().equals("")?pinno.getText().toString().substring(0, (pinno.getText().toString().length()-1)):""));
			}
		});
		Button btnClear = (Button) popupView.findViewById(R.id.ButtonClear);
		btnClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pinno.setText("");
			}
		});

	}
}
